﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SignalR;
using Microsoft.Extensions.DependencyInjection;
using System.Security.Principal;
using System.Threading.Tasks;
using Volo.Abp.AspNetCore.Mvc;
using Volo.Abp.AspNetCore.Mvc.ApplicationConfigurations;
using Volo.Abp.PermissionManagement;
using Volo.Abp.Security.Claims;
using Volo.Abp.Users;

namespace EcoSystem.Controllers;

public class HomeController : AbpController
{
    private readonly ICurrentPrincipalAccessor principalAccessor;
    private readonly PermissionAppService permissionManager;
    public HomeController(ICurrentPrincipalAccessor principalAccessor)
    {
        this.principalAccessor = principalAccessor;
    }

    public ActionResult Index()
    {
#if DEBUG
        return Redirect("~/swagger");
#else
        return Redirect("~/webadmin");
#endif
    }

    [HttpGet]
    [Authorize]
    [Route("currentUser")]
    public ICurrentUser GetCurrentUser()
    {
        var userId = principalAccessor.Principal.FindUserId();
        return this.CurrentUser;
    }


    [HttpPost]
    [Route("/sendMessage")]
    public async Task SendMessage(string message)
    {

        IMessagingHubAppService hub = LazyServiceProvider.GetRequiredService<IMessagingHubAppService>();
        await hub.SendMessageAsync(new HubMessageDto
        {
            MessageType = HubMessageType.Message,
            UserIds = new System.Collections.Generic.List<System.Guid>() {
                System.Guid.Parse("3a0d72d3-d802-35a8-8898-3b22353705e1") ,
            },
            Payload = message
        });
    }
}
